package org.sharelife.dao.impl;

import java.util.List;
import org.sharelife.dao.IMessageDAO;
import org.sharelife.entity.Message;
import org.sharelife.util.PageBean;

@SuppressWarnings("unchecked")
public class MessageDAOImpl extends SuperHibernateDAOImpl implements IMessageDAO{
	
	//根据id删除评论
	@Override
	public void deleteById(int id) {
		super.delete(Message.class, id);
	}
	
	//查询所有评论
	@Override
	public List<Message> findAll() {
		return (List<Message>)super.find("from Message");
	}
	
	//根据发布时间查询前5条记录
	@Override
	public List<Message> findByDate() {
		String hql="from Message order by publishTime desc";
		return super.findPageByQuery(hql, 0, 5);
	}
	
	// 根据ID查询评论
	@Override
	public Message findById(int id) {
		return (Message)super.get(Message.class, id);
	}
	
	// 根据用户ID查询所有评论
	@Override
	public List<Message> findByUserId(int id) {
		return (List<Message>)super.find("from Message m where m.user.id = " + id);
	}
	
	// 修改评论信息
	@Override
	public void modify(Message message) {
		super.modify(message);
	}
	
	// 分页查询
	@Override
	public PageBean queryForPage(int pageSize, int page) {
		final String hql = "from Message order by id desc"; 
		return super.queryForPage(hql, pageSize, page);
	}
	
	//根据关键字分页查询
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize,
			int page) {
		String hql = "from Message "
			+ "where title like '%" + keyWord + "%' "
			+ "or content like '%" + keyWord + "%' "
			+ "or publishTime like '%" + keyWord + "%' "
			+ "or user.userName like '%" + keyWord + "%' "
			+ "or user.email like '%" + keyWord + "%' "
			+ "or user.photoSticker like '%" + keyWord + "%' "
			+ "or user.albums like '%" + keyWord + "%' "
			+ "or user.birthday like '%" + keyWord + "%' ";
		return super.queryForPage(hql, pageSize, page);
	}
	
	// 根据用户ID分页查询评论
	@Override
	public PageBean queryForPageByUserId(int id, int pageSize, int page) {
		final String hql = "from Message where user.id = " + id + " order by id desc"; // 查询语句
		return super.queryForPage(hql, pageSize, page);
	}
	
	//添加评论
	@Override
	public void add(Message message) {
		super.save(message);
	}

	//根据图片id分页查询评论
	@Override
	public PageBean queryForPageByPictureId(int id, int pageSize, int page) {
		final String hql="from Message m where m.picture.id = " + id + " order by id desc";
		return super.queryForPage(hql, pageSize, page);
	}
}
